/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.ad.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.shiro.realm.LoginInfo;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.modules.ad.entity.AdAgent;
import com.jeesite.modules.ad.entity.AdGuest;
import com.jeesite.modules.ad.entity.AdVertiser;
import com.jeesite.modules.ad.service.AdAgentService;
import com.jeesite.modules.ad.service.AdGuestService;
import com.jeesite.modules.ad.service.AdVertiserService;
import com.jeesite.modules.sys.dao.UserRoleDao;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.entity.UserRole;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ad.entity.Ad;
import com.jeesite.modules.ad.service.AdService;

import java.util.ArrayList;
import java.util.List;

/**
 * 广告Controller
 * 
 * @author chenxm
 * @version 2019-04-17
 */
@Controller
@RequestMapping(value = "${adminPath}/ad/ad")
public class AdController extends BaseController {

	@Autowired
	private AdService adService;

	@Autowired
	private AdVertiserService adVertiserService;

	@Autowired
	private AdAgentService adAgentService;

	@Autowired
	private AdGuestService adGuestService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Ad get(String id, boolean isNewRecord) {
		return adService.get(id, isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("ad:ad:view")
	@RequestMapping(value = { "list", "" })
	public String list(Ad ad, Model model) {
		model.addAttribute("ad", ad);
		LoginInfo loginInfo = UserUtils.getLoginInfo();
		AdVertiser curVertiser = this.adVertiserService.getByUserCode(loginInfo.getId());
		if (curVertiser != null) {
			model.addAttribute("isAgent", 1);
		} else {
			AdAgent curAgent = this.adAgentService.getByUserCode(loginInfo.getId());
			if (curAgent != null) {
				model.addAttribute("isAgent", 1);
			} else {
				model.addAttribute("isAgent", 0);
			}
		}
		return "modules/ad/adList";
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("ad:adGuest:view")
	@RequestMapping(value = { "listByAd" })
	public String listByAd(String adName, String adId, Model model) {
		Ad ad = new Ad();
		ad.setName(adName);
		ad.setId(adId);
		model.addAttribute("ad", ad);
		LoginInfo loginInfo = UserUtils.getLoginInfo();
		AdVertiser curVertiser = this.adVertiserService.getByUserCode(loginInfo.getId());
		if (curVertiser != null) {
			model.addAttribute("isAgent", 1);
		} else {
			AdAgent curAgent = this.adAgentService.getByUserCode(loginInfo.getId());
			if (curAgent != null) {
				model.addAttribute("isAgent", 1);
			} else {
				model.addAttribute("isAgent", 0);
			}
		}
		return "modules/ad/adList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ad:ad:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Ad> listData(Ad ad, HttpServletRequest request, HttpServletResponse response) {
		ad.setAd_id_gte();
		ad.setPage(new Page<>(request, response));
		Page<Ad> page = adService.findPage(ad);
		AdGuest adGuest = new AdGuest();
		for (Ad obj : page.getList()) {
			// 查询每条广告的获客数量
			adGuest.setAdId(obj.getId());
			adGuest.setAd(obj);
			Integer findNumByAd = adGuestService.findNumByAd(adGuest);
			obj.setGuestNum(findNumByAd);
		}
		if (ad.getGuestNum() != null) {
			List<Ad> list = new ArrayList<>();
			for (Ad obj : page.getList()) {
				if (ad.getGuestNum() == obj.getGuestNum()) {
					list.add(obj);
				}
			}
			page.setList(list);
		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ad:ad:view")
	@RequestMapping(value = "form")
	public String form(Ad ad, Model model) {
		model.addAttribute("ad", ad);
		LoginInfo loginInfo = UserUtils.getLoginInfo();
		AdVertiser curVertiser = this.adVertiserService.getByUserCode(loginInfo.getId());
		if (curVertiser != null) {
			model.addAttribute("isAgent", 1);
		} else {
			model.addAttribute("isAgent", 0);
		}
		return "modules/ad/adForm";
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ad:ad:view")
	@RequestMapping(value = "details")
	public String details(Ad ad, Model model) {
		model.addAttribute("ad", ad);
		LoginInfo loginInfo = UserUtils.getLoginInfo();
		AdVertiser curVertiser = this.adVertiserService.getByUserCode(loginInfo.getId());
		if (curVertiser != null) {
			model.addAttribute("isAgent", 1);
		} else {
			model.addAttribute("isAgent", 0);
		}
		return "modules/ad/adDetails";
	}

	/**
	 * 保存广告
	 */
	@RequiresPermissions("ad:ad:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Ad ad) {
		adService.save(ad);
		return renderResult(Global.TRUE, text("保存广告成功！"));
	}

	/**
	 * 停用广告
	 */
	@RequiresPermissions("ad:ad:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(Ad ad) {
		ad.setStatus(Ad.STATUS_DISABLE);
		adService.updateStatus(ad);
		return renderResult(Global.TRUE, text("停用广告成功"));
	}

	/**
	 * 启用广告
	 */
	@RequiresPermissions("ad:ad:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(Ad ad) {
		ad.setStatus(Ad.STATUS_NORMAL);
		adService.updateStatus(ad);
		return renderResult(Global.TRUE, text("启用广告成功"));
	}

	/**
	 * 删除广告
	 */
	@RequiresPermissions("ad:ad:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Ad ad) {
		adService.delete(ad);
		return renderResult(Global.TRUE, text("删除广告成功！"));
	}

	/**
	 * 导出广告
	 */
	@RequiresPermissions("ad:ad:edit")
	@RequestMapping(value = "exportData")
	public void exportData(Ad ad, Boolean isAll, String ctrlPermi, HttpServletRequest request,
			HttpServletResponse response) {
		// 是否查询子级
		ad.getAreaCode().setIsQueryChildren(true);
		// 查询当前广告主
		ad.setPage(new Page<>(request, response));
		Page<Ad> page = adService.findPage(ad);
		AdGuest adGuest = new AdGuest();
		for (Ad obj : page.getList()) {
			// 查询每条广告的获客数量
			adGuest.setAdId(obj.getId());
			adGuest.setAd(obj);
			obj.setGuestNum(adGuestService.findNumByAd(adGuest));
		}
		String fileName = "广告信息" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
		try (ExcelExport ee = new ExcelExport("广告信息数据", Ad.class)) {
			ee.setDataList(page.getList()).write(response, fileName);
		}
	}
}